package com.gzyz.cheetah.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzyz.cheetah.config.AdminConfig;
import com.gzyz.cheetah.entity.table.Admin;
import com.gzyz.cheetah.enums.systemconfig.admin.AllowDelete;
import com.gzyz.cheetah.enums.systemconfig.admin.AllowRegister;
import com.gzyz.cheetah.exception.AdminServiceException;
import com.gzyz.cheetah.exception.RegisterException;
import com.gzyz.cheetah.exception.UserServiceException;
import com.gzyz.cheetah.mapper.AdminMapper;
import com.gzyz.cheetah.service.IAdminService;
import com.gzyz.cheetah.util.RedisUtil;
import com.gzyz.cheetah.util.TokenUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static com.gzyz.cheetah.util.SecurePass.securePass;

/**
 * <p>
 * 管理员 服务实现类
 * </p>
 *
 * @author xclhove
 * @since 2023-11-06
 */
@Service
@RequiredArgsConstructor
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    private final AdminConfig adminConfig;
    private final RedisUtil redis;
    
    @Override
    public String login(String adminName, String password) {
        LambdaQueryWrapper<Admin> adminLambdaQueryWrapper = new LambdaQueryWrapper<Admin>()
                .eq(Admin::getUsername,adminName);
        Admin admin = this.getOne(adminLambdaQueryWrapper);
        if (admin == null){
            throw new UserServiceException("账号或密码错误");
        }
        String securePass = securePass(password,adminName);
        if (!securePass.equals(admin.getPassword())) {
            throw new UserServiceException("账号或密码错误");
        }
        final String adminTokenKey = "admin_token";
        String tokenInRedis = redis.getHashString(adminTokenKey, admin.getId());
        boolean validatePass = TokenUtil.validate(tokenInRedis, admin.getPassword());
        if (validatePass) {
            return tokenInRedis;
        }
        //生成token
        String token = TokenUtil.generate(admin.getId(), admin.getPassword());
        redis.setHashString(adminTokenKey, admin.getId(), token);
        return token;
    }
    
    @Override
    public boolean register(Admin admin) {
        if (adminConfig.getAllowRegister() == AllowRegister.DISABLED) {
            throw new AdminServiceException(AllowRegister.DISABLED.getDescription());
        }
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getUsername,admin.getUsername());
        if (this.getOne(lambdaQueryWrapper)!= null){
            throw new RegisterException("该管理员已存在，请跟换用户名！");
        }else {
            String securePass = securePass(admin.getPassword(), admin.getUsername());
            admin.setPassword(securePass);
            return this.save(admin);
        }
    }
    
    @Override
    public boolean logout(Integer adminId) {
        return false;
    }
    
    @Override
    public boolean deleteAdminById(Integer adminId) {
        if (adminConfig.getAllowDelete() == AllowDelete.DISABLED) {
            throw new AdminServiceException(AllowDelete.DISABLED.getDescription());
        }
        return this.removeById(adminId);
    }
    
    @Override
    public boolean updateAdmin(Admin admin) {
        admin.setUpdateTime(LocalDateTime.now());
        return this.updateById(admin);
    }
}
